package com.example.mam.dto;


import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @author muncie [muncie.hoo@qq.com]
 */
public class test1 {
    public static void main(String[] args) throws JsonProcessingException {

        List<String[]> paths = new ArrayList<>();
        String a= "managers\\administrators";
        String b= "managers\\dealers";
        String c= "demo\\demoforex";
        String d= "real\\real";
        String e= "preliminary";
        paths.add(a.split("\\\\"));
        paths.add(b.split("\\\\"));
        paths.add(c.split("\\\\"));
        paths.add(d.split("\\\\"));
        paths.add(e.split("\\\\"));
        TreeNode treeNode = new TreeNode();
        generateTree(paths, treeNode, 0);
        ObjectMapper mapper = new ObjectMapper();
        String mapJakcson = mapper.writeValueAsString(treeNode);
        System.out.println(mapJakcson);
    }

    public static void generateTree(List<String[]> paths, TreeNode node, int layer) {
        // 将组整理成路径
        Set<TreeNode> children = new HashSet<>();
        for (String[] path : paths) {
            if (path.length > layer) {
                if (layer == 0 || node.getLabel().equals(path[layer - 1])) {
                    TreeNode treeNode = new TreeNode();
                    treeNode.setLabel(path[layer]);
                    generateTree(paths, treeNode, layer + 1);
                    children.add(treeNode);
                }
            }
        }
        if (children.size() != 0) {
            node.setChildren(children);
        }
    }
}
